package com.example.administrator.capital.mine.second;


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.eventbus.EventBase;
import com.example.administrator.capital.presenter.MinePresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.PreferenceManager;
import com.example.administrator.capital.utils.ToastUtil;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import eventbusactivityscope.EventBusActivityScope;

/**
 * A simple {@link Fragment} subclass.
 * 修改昵称
 */
public class AmendNickNameFragment extends BaseBackFragment {

    @BindView(R.id.clear)
    ImageView mClear;
    Unbinder unbinder;
    @BindView(R.id.amend)
    EditText mAmend;
    private TextView mBack;
    private MinePresenter minePresenter;
    private TextView commit;

    public AmendNickNameFragment() {
        // Required empty public constructor
    }

    public static AmendNickNameFragment newInstance() {

        Bundle args = new Bundle();
        AmendNickNameFragment fragment = new AmendNickNameFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_amend_nick_name, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mBack = view.findViewById(R.id.app_title_back);
        commit=view.findViewById(R.id.app_title_action);
        minePresenter=new MinePresenter(null);
    }

    private void getDate() {
        minePresenter.updatemean(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {

            }

            @Override
            public void onCode(int code) {

            }

            @Override
            public void onSuccess(Response<String> data) {
                EventBusActivityScope.getDefault(getActivity()).post(new EventBase("刷新界面"));
                ToastUtil.showShort("修改成功");
                _mActivity.onBackPressed();
            }
            @Override
            public void onFail(Response<String> error) {

            }
        },mAmend.getText().toString().trim(),"","");
    }

    @Override
    protected void setListener() {
        super.setListener();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();

            }
        });
        mClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAmend.setText("");
            }
        });
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDate();
            }
        });

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
